<!--添加生产数据-->
<template>
  <div class="add-produce-data-wrap">
    <addDetail title="添加生产数据" type="add" :auto="auto" :produceDataDetail="produceDataDetail" @update="getData" @autofill="getFillData" />

    <loading class="add-produce-percentage" :show="show1" :text="text1"> </loading>
    <toast v-model="show2" width="14em">
      <div>
        <p>{{ text1 }}</p>
        <p>{{ text2 }}</p>
      </div>
    </toast>
  </div>
</template>

<script>
import ProduceDataDao from '@/daos/produce_data_dao';
import addDetail from './addDetail.vue';
import { Loading, Toast, TransferDomDirective as TransferDom } from 'vux';
export default {
  name: 'addProduceData',
  directives: {
    TransferDom,
  },
  data() {
    return {
      produceDataDetail: {
        departmentId: '',
        fillTime: '',
        departmentName: '',
        orgId: '',
        orgJobShiftId: '',
        orgJobShiftName: '',
        members: '',
        userName: '',
        userId: '',
        status: '',
        output: '',
        sectionList: [],
      },
      show1: false,
      show2: false,
      text1: '',
      text2: '',
      timer: null,
      auto: 0,
      percentage: 0,
      hasFillData: 0,
      store: {},
    };
  },

  components: {
    addDetail,
    Loading,
    Toast,
  },

  computed: {},

  mounted() {
    this.store.produceDataDetail = JSON.parse(JSON.stringify(this.produceDataDetail));
    let res = JSON.parse(localStorage.getItem('addProduceData'));
    this.auto = +this.$route.query.auto || 0;
    if (res && res.status === 2) {
      this.produceDataDetail = res;
    } else if (this.$store.state.user) {
      if (this.$store.state.user.serviceAreaTree.children.length && !this.auto) {
        this.getData(this.$store.state.user.serviceAreaTree.children[0].id);
      }
    }
  },

  methods: {
    getData(departmentId) {
      ProduceDataDao.getProduceConfig(departmentId, (res) => {
        this.produceDataDetail = JSON.parse(JSON.stringify(this.store.produceDataDetail));
        this.fillData(res);
      });
    },
    getFillData(tag = 0) {
      // 显示
      this.show1change();
      ProduceDataDao.getFillData(
        {
          dataTime: this.produceDataDetail.fillTime,
          departmentId: this.produceDataDetail.departmentId,
          orgJobShiftId: this.produceDataDetail.orgJobShiftId,
        },
        (res) => {
          if (!tag) {
            this.produceDataDetail = JSON.parse(JSON.stringify(this.store.produceDataDetail));
          }
          this.fillData(res);
          if (!tag) {
            this.text1 = this.hasFillData ? '获取成功' : '未获取到上报数据';
            this.text2 = this.hasFillData ? '5s内跳转到添加数据页' : '请手动添加数据';
          } else {
            this.text1 = this.hasFillData ? '获取成功' : '未获取到上报数据';
          }
          this.show2 = true;
          this.show1 = false;
          clearInterval(this.timer);
          setTimeout(() => {
            this.auto = 2;
            this.show2 = false;
          }, 1000);
        }
      );
    },
    fillData(res) {
      this.hasFillData = 0;
      if (res) {
        let autocomplete = {};
        this.produceDataDetail.id = '';
        this.produceDataDetail.departmentId = res.departmentId;
        this.produceDataDetail.fillTime = res.fillTime;
        this.produceDataDetail.departmentName = res.departmentName;
        this.produceDataDetail.orgId = res.orgId;
        this.produceDataDetail.orgJobShiftId = res.orgJobShiftId;
        this.produceDataDetail.orgJobShiftName = res.orgJobShiftName;
        this.produceDataDetail.members = res.members || '';
        this.produceDataDetail.userName = res.userName;
        this.produceDataDetail.userId = res.userId;
        this.produceDataDetail.status = '1';
        res.sectionList.forEach((item, index) => {
          item.detailList = item.sectionSeriesList || [];
          item.detailList.forEach((sItem, sIndex) => {
            sItem.processSectionSeriesName = sItem.seriesName;
            sItem.processSectionSeriesId = sItem.id;
            sItem.processName = sItem.processSectionName;
            sItem.processId = sItem.processSectionId;
            sItem.remark = '';
            sItem.data = '';
            // sItem.parkingList = [{id: '',productFillDataDetailId: '',startTime: '',endTime: '',remark: ''}]
            sItem.parkingList = [];
            sItem.seriesParamsArray = [];
            sItem.sectionFieldList &&
              sItem.sectionFieldList.forEach((ssItem, ssIndex) => {
                if (ssItem.fillData && !this.hasFillData) {
                  this.hasFillData++;
                }
                let value = '';
                if (this.produceDataDetail.sectionList[index] && this.produceDataDetail.sectionList[index].detailList[sIndex]) {
                  value = this.produceDataDetail.sectionList[index].detailList[sIndex].seriesParamsArray[ssIndex].value || '';
                }
                if (ssItem.fillData) {
                  value = ssItem.fillData;
                }
                sItem.seriesParamsArray.push({ name: ssItem.name, value, fieldCode: ssItem.fieldCode, statisticType: ssItem.statisticType, inputDataType: ssItem.inputDataType, inputDataMaxValue: ssItem.inputDataMaxValue });

                // 需增加自动计算:原矿处理量=精料班产量+甩废班产量
                if (value && (ssItem.fieldCode === 'jingliao_quantity' || ssItem.fieldCode === 'shaifei_quantity')) {
                  autocomplete[ssItem.fieldCode] = value;
                }

                if (ssItem.fieldCode === 'yuankuang_rhandle_quantity') {
                  autocomplete[ssItem.fieldCode] = [index, sIndex, ssIndex];
                }
              });
          });
        });

        if (autocomplete['jingliao_quantity'] && autocomplete['shaifei_quantity']) {
          let index = autocomplete['yuankuang_rhandle_quantity'];
          let sum = (+autocomplete['jingliao_quantity'] || 0) + (+autocomplete['shaifei_quantity'] || 0);
          if (index.length) {
            res.sectionList[index[0]].detailList[index[1]].seriesParamsArray[index[2]].value = sum;
          }
        }
        this.produceDataDetail.sectionList = res.sectionList;
      }
    },
    show1change() {
      this.show1 = true;
      this.timer = setInterval(() => {
        this.percentage++;
        if (this.percentage < 80) {
          this.text1 = `${this.percentage}%`;
        }
      }, 100);
    },
  },
};
</script>
<style lang="less" scoped>
.add-produce-data-wrap {
  background: #1c1f37;
  min-height: 100%;
}
</style>
<style lang="less">
.add-produce-percentage {
  .weui-toast__content {
    font-size: 14px;
  }
}
</style>
